package com.xiaoq.mongotest;

import java.util.ArrayList;
import java.util.List;

import org.mongodb.morphia.Datastore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericResponse;

@RestController
@RequestMapping("/api/v1/montotest")
public class TestEndpoint {
	private static final int USER_COUNT = 50;
	private static final int COMMENT_COUNT = 50;
	private static final int ORDER_ITEM_COUNT = 1000;
	@Autowired
	private Datastore ds;
	
	@PostMapping("/init_data")
	public ResponseEntity<?> initData() {
		for (int i = 0; i < USER_COUNT; i++) {
			CommentUserEntity cue = new CommentUserEntity("userName" + i, "userNickName" + i, "userLogo" + i);
			cue.setId("userId" + i);
			ds.save(cue);
		}
		for (int i = 0; i < ORDER_ITEM_COUNT; i++) {
			OrderItemEntity oie = new OrderItemEntity("orderItemDesc " + i, 10.0+i,i,(10.0+i)*i);
			oie.setId("orderItemId" + i);
			ds.save(oie);
			List<CommentVo> clist = new ArrayList<CommentVo>();
			for (int j = 0; j < COMMENT_COUNT; j++) {
				CommentEntity ce = new CommentEntity("userId" + j, "userName" + j, "userNickName" + j, "userLogo" + j, "orderItemId" + i, "orderItemDesc" + i, "content userId" + j);
				ce.setId("comment" + (i * 100000 + j)); 
				ds.save(ce);
				CommentVo cv = new CommentVo("userId" + j, "userName" + j, "userNickName" + j, "userLogo" + j, "orderItemId" + i, "orderItemDesc" + i, "content userId" + j);
				clist.add(cv);
			}
			OrderItemWithCommentEntity oiwce = new OrderItemWithCommentEntity("orderItemDesc " + i, 10.0+i, i, (10.0+i)*i, clist);
			oiwce.setId("orderItemId" + i);
			ds.save(oiwce);
		}
		return GenericResponse.ok();
	}
	
	@GetMapping("/get_order_item")
	public ResponseEntity<?> getOrderItem(String id) {
		System.out.println(System.currentTimeMillis());
		OrderItemEntity oie = ds.get(OrderItemEntity.class, id);
		System.out.println(System.currentTimeMillis());
		System.out.println(System.currentTimeMillis());
		List<CommentEntity> list = ds.find(CommentEntity.class, "orderItemId", id).asList();
		System.out.println(list.size());
		System.out.println(System.currentTimeMillis());
		return GenericResponse.ok();
	}
	
	@GetMapping("/get_order_item_with_comment")
	public ResponseEntity<?> getOrderItemWithComment(String id) {
		System.out.println(System.currentTimeMillis());
		OrderItemWithCommentEntity oiwce = ds.get(OrderItemWithCommentEntity.class, id);
		System.out.println(oiwce.getComments().size());
		System.out.println(System.currentTimeMillis());
		return GenericResponse.ok();
	}
	
	@GetMapping("/get_comment_of_order_item")
	public ResponseEntity<?> getCommentOfOrderItem(String id) {
		System.out.println(System.currentTimeMillis());
		List<CommentEntity> list = ds.find(CommentEntity.class, "orderItemId", id).asList();
		System.out.println(System.currentTimeMillis());
		return GenericResponse.ok();
	}
}
